package com.hilin.test.fs;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.freeswitch.esl.client.outbound.AbstractOutboundClientHandler;
import org.freeswitch.esl.client.outbound.AbstractOutboundPipelineFactory;
import org.freeswitch.esl.client.outbound.SocketClient;
import org.freeswitch.esl.client.test.FsClient;
import org.freeswitch.esl.client.test.FsClientUtils;
import org.freeswitch.esl.client.test.FsEventType;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Slf4j
@SpringBootApplication
public class Application implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Autowired
    private FsEventHandler fsEventHandler;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        inbound();
        outbound();
    }

    private void outbound() {
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                final SocketClient outboundServer = new SocketClient(
                        19090,
                        new AbstractOutboundPipelineFactory() {
                            @Override
                            protected AbstractOutboundClientHandler makeHandler() {
                                return new AbstractOutboundClientHandler() {
                                    @Override
                                    protected void handleConnectResponse(ChannelHandlerContext ctx, EslEvent event) {
                                        log.info("==========================handleConnectResponse==========================\n" + JSON.toJSONString(event));
                                    }

                                    @Override
                                    protected void handleEslEvent(ChannelHandlerContext ctx, EslEvent event) {
                                        log.info("==========================收到事件==========================\n" + JSON.toJSONString(event));
                                    }
                                };
                            }
                        });
                outboundServer.start();
            }
        }).start();
    }

    void inbound() {
        new Thread(new Runnable() {
            @Override
            @SneakyThrows
            public void run() {
                FsClient currentClient = new FsClient();
                currentClient.connect("192.168.0.86", 8021, "ClueCon", 10);
                FsClientUtils.setEventFilter(currentClient, FsEventType.values());
                log.debug("开始注册事件监听器");
                currentClient.addEventListener(fsEventHandler);
            }
        }).start();
    }


}
